package com.webemail.views.controller;

import com.webemail.sys.model.SysContact;
import com.webemail.sys.service.ContactService;
import com.webemail.sys.util.CommonUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author LaiHQ
 */
@Controller
@RequestMapping("/contact")
public class ContactControllerView {
	@Resource
	private ContactService contactService;
	private long getUserId() {
		return 1;
	}

	@RequestMapping("/add")
	public String add(HttpServletRequest request, Model model) {
		return "mail/contact_add";
	}

	@RequestMapping("/update")
	public String update(HttpServletRequest request, Model model) {
		Long userId = this.getUserId();
		Long id;
		try {
			id = Long.parseLong(CommonUtils.getString(request.getParameter("id")).trim());
		} catch (Exception e) {
			model.addAttribute("msg", "请选择联系人");
			return "mail/error";
		}
		SysContact temp = contactService.get(id, userId);
		if (temp == null || temp.getId() < 1) {
			model.addAttribute("msg", "联系人不存在");
			return "mail/error";
		}
		model.addAttribute("contact", temp);
		return "mail/contact_update";
	}

	@RequestMapping("/all")
	public String all(HttpServletRequest request, Model model) {
		return "mail/contact_all";
	}
}
